package terra.model.effect;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Vector2f;

import terra.Config;
import terra.managers.DefManager;
import terra.managers.LightManager;
import terra.managers.ResourceManager;
import terra.model.MapObject;
import terra.slick.GameState;
import terra.util.FontUtils;

public class HitBlockEffect extends AbstractEffect {

	int x,y,w,h,y1,damage;
	boolean red = false;
	Vector2f main;
	String img;
	ArrayList<Vector2f> list = new ArrayList<Vector2f>();
	public HitBlockEffect(MapObject obj, int damage) {
		time = 2000;
		img = DefManager.groundDefs.get(obj.id).image;
		x = (int) obj.x*Config.tileSize+Config.tileSize/2;
		y = (int) obj.y*Config.tileSize+Config.tileSize/2;
		main = new Vector2f(x,y);
		for(int i = 5+GameState.rnd.nextInt(10);i>0;i--)
			list.add(new Vector2f(x+(-8+GameState.rnd.nextInt(16)),y+(-8+GameState.rnd.nextInt(16))));
		w = Config.tileSize;
		h = Config.tileSize;
		this.damage = damage;
	}
	float gravity = 0;
	public void draw(Graphics g) {
		float alpha = (time/(2000/100f))/100f;
		gravity+=Config.gravity/60;
		//alpha = 1-alpha;
		Color c = new Color(Color.red.getRed(),Color.red.getGreen(),Color.red.getBlue(),alpha);
		FontUtils.drawCenter(GameState.font12, String.valueOf(damage), x, y+y1-16, w, c);
		y-= 0.1f;
		Image image = ResourceManager.getImage(img);
		LightManager.eraseBlend(image);
		for(Vector2f v2f : list){
			image.draw(v2f.x, v2f.y,4,4,new Color(Color.white.getRed(),Color.white.getGreen(),Color.white.getBlue(),alpha));
			v2f.add(new Vector2f(v2f.x-main.x,v2f.y-main.y).scale(0.02f));
			v2f.add(new Vector2f(0,gravity).scale(0.02f));
		}
	}

	@Override
	public void end() {
		remove = true;
		list.clear();
		list = null;
	}

}
